#!/bin/bash

# Slackware build script for stretchplayer

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# Upstream git is: https://gitlab.com/stretchplayer/stretchplayer.git
# Latest commits there are post 0.503 release, development
# stopped in 2014. Lots of nice new features there, including mp3
# support. Unfortunately building the latest git code results in a
# stretchplayer that segfaults on startup, and I don't have a fix, so
# I didn't try to package that. I did cherry-pick one commit though.

##### TODO:
# on every exit (close with the close button):
# corrupted double-linked list
# Aborted

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=stretchplayer
VERSION=${VERSION:-0.503_4}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

SRCVER="$( echo $VERSION | cut -d_ -f1 )"
DEBVER="$( echo $VERSION | cut -d_ -f2 )"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
tar xvf $CWD/${PRGNAM}_$SRCVER.orig.tar.gz
cd $PRGNAM-$SRCVER
tar xvf $CWD/${PRGNAM}_$SRCVER-$DEBVER.debian.tar.xz
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# Apply Debian's patches, especially qt5.diff!
for i in $( cat debian/patches/series ); do
  patch -p1 < debian/patches/$i
done

# Patch by SlackBuild author. Allows tuning up and down by 10 cents
# (1/10 of a semitone) when holding the Shift key, or 1 cent when
# holding Control. Helps with stuff that was recorded slightly out of
# tune, or mastered from ancient tapes. Patch was sent upstream, but
# upstream isn't responding.
patch -p1 < $CWD/fine_tuning.diff

# Patch from upstream git, changes the stretch range from 25%-125%.
# commit a3e0f1f.
patch -p1 < $CWD/stretch_25_125.diff

mkdir -p build
cd build
  cmake \
    -DENABLE_UNIT_TESTS=OFF \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DLIB_SUFFIX=${LIBDIRSUFFIX} \
    -DMAN_INSTALL_DIR=/usr/man \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install/strip DESTDIR=$PKG
cd ..

# man page by SlackBuild author. Based on the Debian one, but
# includes a list of keystrokes.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# I don't think stretchplayer needs this at runtime:
rm -rf $PKG/usr/share/$PRGNAM $PKG/usr/share/icons

for i in art/$PRGNAM-icon-*.png; do
  size="$( basename $i .png | cut -d- -f3 )"
  mkdir -p $PKG/usr/share/icons/hicolor/$size/apps
  cp -a $i $PKG/usr/share/icons/hicolor/$size/apps/$PRGNAM.png
done
mkdir -p $PKG/usr/share/icons/hicolor/scalable/apps
cp -a art/$PRGNAM-icon.svg $PKG/usr/share/icons/hicolor/scalable/apps/$PRGNAM.svg

rm $PKG/usr/share/pixmaps/*
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

sed -i "/^Icon/s,=.*,=$PRGNAM," $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS BUGS* COPYING ChangeLog README* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

if [ "${SETCAP:-yes}" = "yes" ]; then
  cat $CWD/setcap.sh >> $PKG/install/doinst.sh
  chown root:audio $PKG/usr/bin/$PRGNAM
  chmod 0750 $PKG/usr/bin/$PRGNAM
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
